<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use App\Models\ApplicationModel;

class AuthController extends Controller
{
    use ModelForm;
    
   
    
    public function edit(){
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }
    public function show(){
        
    }
    public function reset(Request $request){
        $credentials = $request->only(['username', 'password','repassword']);

        $validator = Validator::make($credentials, [
            'username' => 'required', 'password' => 'required', 'repassword' => 'required',
        ]);

        if ($validator->fails()) {
            return Redirect::back()->withInput()->withErrors($validator);
        }
        if($request->get('password')!=$request->get('repassword')){
            return Redirect::back()->withInput()->withErrors();
        }else{
            $oApp = new ApplicationModel();
            $re = $oApp->setPassword($request->get('username'),$request->get('password'));
            if($re>0){
                return redirect(admin_url('auth/login'));
            }
            else{
                return false;
            }
        }
//        var_dump($request->get('username'));
//        var_dump($request->get('password'));
//        var_dump($request->get('repassword'));
//        
//        echo "done";
    }
    public function forget(){
//        var_dump("$$$$$$");
//        return Admin::content(function (Content $content) {
//            $content->header(trans('admin::lang.resetpassword'));
//            $content->body($this->settingForm());
//        
         return view('admin::reset');
    }
    
    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm()
    {
        return Administrator::form(function (Form $form) {
            $form->display('username', trans('admin::lang.username'));
            $form->text('name', trans('admin::lang.name'))->rules('required');
            $form->image('avatar', trans('admin::lang.avatar'));
            $form->password('password', trans('admin::lang.password'))->rules('confirmed|required');
            $form->password('password_confirmation', trans('admin::lang.password_confirmation'))->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });

            $form->setAction(admin_url('auth/setting'));

            $form->ignore(['password_confirmation']);

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });

            $form->saved(function () {
                admin_toastr(trans('admin::lang.update_succeeded'));

                return redirect(admin_url('auth/setting'));
            });
        });
    }
}
